/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fatclientnbp;

import AirLine.Booking;
import Database.Airlines;
import Database.Airports;
import Model.Trip;
import Model.TripHandler;
import edu.chl.chalmers.fatclient.sb.FatClientSBRemote;
import edu.chl.chalmers.fatclient.sb.Flight;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Observable;
import java.util.TreeSet;

/**
 *
 * @author Fredrik
 */
public class MainModel extends Observable {

    public final static int SEARCH_STATE = 0;
    public final static int SELECT_STATE = 1;
    public final static int BOOK_STATE = 2;
    public final static int FINISHED_STATE = 3;
    private Airports airports;
    private Airlines airlines;
    private SearchParameters searchParameters;
    private int state;
    private Trip chosenTrip;
    private static FatClientSBRemote fatClientSBBean;
    private TripHandler th;
    private ArrayList<Booking> bookings;

    public MainModel() {
        airlines = new Airlines();
        airports = new Airports();
        state = SEARCH_STATE;
    }

    public Airlines getAirlines() {
        return airlines;
    }

    public Airports getAirports() {
        return airports;
    }
    
    public Trip getChosenTrip()
    {
        return chosenTrip;
    }

    public SearchParameters getSearchParameters()
    {
        return searchParameters;
    }
    
    public void book(ArrayList<Booking> b) {
        this.bookings = b;
        System.out.println("booking");
        fatClientSBBean.book(b);
        
    }

    public void setSearchParameters(SearchParameters searchParameters) {
        this.searchParameters = searchParameters;
    }
    public ArrayList<Booking> getBookings() {
        return bookings;
    }
    public void setState(int state) {
        this.state = state;
        setChanged();
        notifyObservers();
    }

    public void setTrip(Trip chosenTrip) {
        this.chosenTrip = chosenTrip;

    }

    public int getState() {
        return state;
    }

    TreeSet<Trip> getTrips() {

        fatClientSBBean = Main.getFatClientSBRemote();

        if (fatClientSBBean == null) {
            System.out.println("fuck");
        }

        TreeSet<Flight> flights = fatClientSBBean.getFlights();
        Iterator it = searchParameters.getAirlines().iterator();
        while (it.hasNext()) {
            
        
        System.out.println(it.next());
        }
                th = new TripHandler(searchParameters.getFrom(), searchParameters.getTo(), searchParameters.getPassangers(), searchParameters.getSortingOrder(), searchParameters.getEarliestDepartureTime(), airports, flights, searchParameters.getAirlines());
        try {
            return th.getTrips();
        } catch (SQLException e) {
            System.out.println(e.getStackTrace());

            return null;

        }
    }
}
